package com.example.tian.securityinspection.ui.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.example.tian.securityinspection.R;
import com.example.tian.securityinspection.base.BaseActivity;
import com.example.tian.securityinspection.base.BaseController;
import com.example.tian.securityinspection.context.AppContext;
import com.example.tian.securityinspection.controller.TestController;
import com.example.tian.securityinspection.gen.FruitDao;
import com.example.tian.securityinspection.model.bean.ResponseError;
import com.example.tian.securityinspection.model.bean.genBean.Fruit;
import com.example.tian.securityinspection.util.ContentView;
import com.example.tian.securityinspection.util.genUtil.FruitDaoUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

@ContentView(R.layout.activity_test)
public class TestActivity extends BaseActivity<TestController.TestUiCallbacks>
        implements TestController.TestUi {

    private static final String TAG = "TestActivity";
    @BindView(R.id.insertsingle)
    Button mInsertsingle;
    @BindView(R.id.multinsert)
    Button mMultinsert;
    @BindView(R.id.update)
    Button mUpdate;
    @BindView(R.id.deletesingle)
    Button mDeletesingle;
    @BindView(R.id.deleteMult)
    Button mDeleteMult;
    @BindView(R.id.checksingle)
    Button mChecksingle;
    @BindView(R.id.checkmult)
    Button mCheckmult;
    @BindView(R.id.querybuilder)
    Button mQuerybuilder;

    private FruitDaoUtil mFruitUtil;

    @Override
    protected BaseController getController() {
        return AppContext.getContext().getMainController().getTestController();
    }

    /**
     * 初始化view
     *
     * @param savedInstanceState
     */
    @Override
    protected void initializeViews(Bundle savedInstanceState) {
        super.initializeViews(savedInstanceState);
        mFruitUtil = new FruitDaoUtil(this);
    }




    @Override
    public void onResponseError(ResponseError error) {
        super.onResponseError(error);
    }

    @Override
    public void onFinish() {

    }

    /**
     * 点击事件
     *
     * @param view
     */    @OnClick({R.id.insertsingle, R.id.multinsert, R.id.update, R.id.deletesingle, R.id.deleteMult, R.id.checksingle, R.id.checkmult, R.id.querybuilder})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.insertsingle:
                mFruitUtil.insertFruit(new Fruit(null,"苹果",12));
                break;
            case R.id.multinsert:

                List<Fruit> fruitList = new ArrayList<>();
                fruitList.add(new Fruit(null,"橘子",14));
                fruitList.add(new Fruit(null,"西瓜",143));
                fruitList.add(new Fruit(null,"栗子",124));
                mFruitUtil.insertListFruit(fruitList);
                break;
            case R.id.update:
                Fruit fruit = new Fruit();
                fruit.setId((long) 10021);
                fruit.setName("榴莲");
                mFruitUtil.updateFruit(fruit);
                break;
            case R.id.deletesingle:
                Fruit fruit1 = new Fruit();
                fruit1.setId((long) 10021);
                mFruitUtil.deleteFruit(fruit1);
                break;
            case R.id.deleteMult:
                mFruitUtil.deleteAll();
                break;
            case R.id.checksingle:
                Log.i(TAG,mFruitUtil.queryFruitById(1008l).toString());
                break;
            case R.id.checkmult:
                List<Fruit> fruitList1 = mFruitUtil.queryAllFruit();
                for (Fruit fruit2: fruitList1){
                    Log.i(TAG, fruit2.toString());
                }
                break;
            case R.id.querybuilder:
//                String sql = "where _id > ?";
//                String[] condition = new String[]{"1008"};
//                List<Fruit> fruitList2 = mFruitUtil.queryFruitByQueryBuilder(sql, condition);
                List<Fruit> fruitList2 = mFruitUtil.queryFruitByQueryBuilder(1008);
                for (Fruit fruit2 : fruitList2) {
                    Log.i(TAG, fruit2.toString());
                }
                break;
        }
    }
}
